package com.itheima.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.tanhua.dubbo.mapper.UserInfoMapper;
import com.itheima.tanhua.dubbo.mapper.UserMapper;
import com.itheima.tanhua.model.domain.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long userId) {
        return userInfoMapper.selectById(userId);
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo user) {
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();

        if (user != null) {
            lqw.lt(user.getAge() != null, UserInfo::getAge, user.getAge());
            lqw.eq(user.getGender() != null, UserInfo::getGender, user.getGender());
        }

        lqw.in(userIds != null, UserInfo::getId, userIds);

        List<UserInfo> userInfoList = userInfoMapper.selectList(lqw);

        //将集合中对象的id值作为key，userinfo对象作为value
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(userInfoList, "id");

        return map;
    }

//    @Override
//    public List<UserInfo> findByIds(List<Long> userIds, UserInfo user) {
//        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
//        lqw.lt(user.getAge()!=null,UserInfo::getAge,user.getAge());
//        lqw.eq(user.getGender()!=null,UserInfo::getGender,user.getGender());
//        lqw.in(userIds!=null,UserInfo::getId,userIds);
//
//        List<UserInfo> userInfoList = userInfoMapper.selectList(lqw);
//
//        return userInfoList;
//    }
}
